/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.service.investorfinancialinfo;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.base.dao.investorfinancialinfo.*;
import com.jeesite.modules.base.entity.investorfinancialinfo.*;
import com.jeesite.modules.file.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资方金融费率维护Service
 *
 * @author LV
 * @version 2021-03-03
 */
@Service
@Transactional(readOnly = true)
public class InvestorFinancialInfoService extends CrudService < InvestorFinancialInfoDao, InvestorFinancialInfo > {

    @Autowired
    private LcFinancialRatioDao lcFinancialRatioDao;
    @Autowired
    private TtFinancialRatioDao ttFinancialRatioDao;
    @Autowired
    private NegotiatedInterestDao negotiatedInterestDao;
    @Autowired
    private PadTaxInterestDao padTaxInterestDao;

    /**
     * 获取单条数据
     *
     * @param investorFinancialInfo
     * @return
     */
    @Override
    public InvestorFinancialInfo get(InvestorFinancialInfo investorFinancialInfo) {
        InvestorFinancialInfo entity = super.get(investorFinancialInfo);
        if (entity != null) {
            LcFinancialRatio lcFinancialRatio = new LcFinancialRatio(entity);
            lcFinancialRatio.setStatus(LcFinancialRatio.STATUS_NORMAL);
            entity.setLcFinancialRatioList(lcFinancialRatioDao.findList(lcFinancialRatio));
            TtFinancialRatio ttFinancialRatio = new TtFinancialRatio(entity);
            ttFinancialRatio.setStatus(TtFinancialRatio.STATUS_NORMAL);
            entity.setTtFinancialRatioList(ttFinancialRatioDao.findList(ttFinancialRatio));
            NegotiatedInterest negotiatedInterest = new NegotiatedInterest(entity);
            negotiatedInterest.setStatus(NegotiatedInterest.STATUS_NORMAL);
            entity.setNegotiatedInterestList(negotiatedInterestDao.findList(negotiatedInterest));
            PadTaxInterest padTaxInterest = new PadTaxInterest(entity);
            padTaxInterest.setStatus(PadTaxInterest.STATUS_NORMAL);
            entity.setPadTaxInterestList(padTaxInterestDao.findList(padTaxInterest));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param investorFinancialInfo      查询条件
     * @param investorFinancialInfo.page 分页对象
     * @return
     */
    @Override
    public Page < InvestorFinancialInfo > findPage(InvestorFinancialInfo investorFinancialInfo) {
        return super.findPage(investorFinancialInfo);
    }

    /**
     * 保存数据（插入或更新）
     *
     * @param investorFinancialInfo
     */
    @Override
    @Transactional(readOnly = false)
    public void save(InvestorFinancialInfo investorFinancialInfo) {
        super.save(investorFinancialInfo);
        // 保存上传附件
        FileUploadUtils.saveFileUpload(investorFinancialInfo.getId(), "investorFinancialInfo_file");
        // 保存 InvestorFinancialInfo子表
        for (LcFinancialRatio lcFinancialRatio : investorFinancialInfo.getLcFinancialRatioList()) {
            if (!LcFinancialRatio.STATUS_DELETE.equals(lcFinancialRatio.getStatus())) {
                lcFinancialRatio.setInvestorFinancialInfoId(investorFinancialInfo);
                if (lcFinancialRatio.getIsNewRecord()) {
                    lcFinancialRatioDao.insert(lcFinancialRatio);
                } else {
                    lcFinancialRatioDao.update(lcFinancialRatio);
                }
            } else {
                lcFinancialRatioDao.delete(lcFinancialRatio);
            }
        }
        // 保存 InvestorFinancialInfo子表
        for (TtFinancialRatio ttFinancialRatio : investorFinancialInfo.getTtFinancialRatioList()) {
            if (!TtFinancialRatio.STATUS_DELETE.equals(ttFinancialRatio.getStatus())) {
                ttFinancialRatio.setInvestorFinancialInfoId(investorFinancialInfo);
                if (ttFinancialRatio.getIsNewRecord()) {
                    ttFinancialRatioDao.insert(ttFinancialRatio);
                } else {
                    ttFinancialRatioDao.update(ttFinancialRatio);
                }
            } else {
                ttFinancialRatioDao.delete(ttFinancialRatio);
            }
        }
        // 保存 InvestorFinancialInfo子表
        for (NegotiatedInterest negotiatedInterest : investorFinancialInfo.getNegotiatedInterestList()) {
            if (!NegotiatedInterest.STATUS_DELETE.equals(negotiatedInterest.getStatus())) {
                negotiatedInterest.setInvestorFinancialInfoId(investorFinancialInfo);
                if (negotiatedInterest.getIsNewRecord()) {
                    negotiatedInterestDao.insert(negotiatedInterest);
                } else {
                    negotiatedInterestDao.update(negotiatedInterest);
                }
            } else {
                negotiatedInterestDao.delete(negotiatedInterest);
            }
        }
        // 保存 InvestorFinancialInfo子表
        for (PadTaxInterest padTaxInterest : investorFinancialInfo.getPadTaxInterestList()) {
            if (!PadTaxInterest.STATUS_DELETE.equals(padTaxInterest.getStatus())) {
                padTaxInterest.setInvestorFinancialInfoId(investorFinancialInfo);
                if (padTaxInterest.getIsNewRecord()) {
                    padTaxInterestDao.insert(padTaxInterest);
                } else {
                    padTaxInterestDao.update(padTaxInterest);
                }
            } else {
                padTaxInterestDao.delete(padTaxInterest);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param investorFinancialInfo
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(InvestorFinancialInfo investorFinancialInfo) {
        super.updateStatus(investorFinancialInfo);
    }

    /**
     * 删除数据
     *
     * @param investorFinancialInfo
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(InvestorFinancialInfo investorFinancialInfo) {
        super.delete(investorFinancialInfo);
        LcFinancialRatio lcFinancialRatio = new LcFinancialRatio();
        lcFinancialRatio.setInvestorFinancialInfoId(investorFinancialInfo);
        lcFinancialRatioDao.deleteByEntity(lcFinancialRatio);
        TtFinancialRatio ttFinancialRatio = new TtFinancialRatio();
        ttFinancialRatio.setInvestorFinancialInfoId(investorFinancialInfo);
        ttFinancialRatioDao.deleteByEntity(ttFinancialRatio);
        NegotiatedInterest negotiatedInterest = new NegotiatedInterest();
        negotiatedInterest.setInvestorFinancialInfoId(investorFinancialInfo);
        negotiatedInterestDao.deleteByEntity(negotiatedInterest);
        PadTaxInterest padTaxInterest = new PadTaxInterest();
        padTaxInterest.setInvestorFinancialInfoId(investorFinancialInfo);
        padTaxInterestDao.deleteByEntity(padTaxInterest);
    }

    public InvestorFinancialInfo getOneByID(String investorFinancialInfoId) {
        InvestorFinancialInfo where = new InvestorFinancialInfo();
        where.setInvestorFinancialInfoId(investorFinancialInfoId);
        return dao.getByEntity(where);
    }

}